package 线性结构.util;

public class LinkedList<E> implements IList<E> {

    private int size;
    private Node<E> first = null;

    private static class Node<E> {
        E element;
        Node<E> next;

        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node node = first;
        if (index == 0) {
            first = first.next;
        } else {
            Node<E> preNode = getNodeByIndex(index - 1);
            node = preNode.next;
            preNode.next = node.next;

        }
        size--;
        return (E) node.element;
    }

    @Override
    public void add(int index, E element) {
        if (index == 0) {
            first = new Node<E>(element, first);
        } else {
            Node<E> preNode = getNodeByIndex(index - 1);
            preNode.next = new Node<>(element, preNode.next);
        }
        size++;
    }

    @Override
    public void set(int index, E element) {
        getNodeByIndex(index).element = element;
    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public void clear() {
        size = 0;
        first.next = null;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            Node node = first;
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) return i;
                node = node.next;
            }
        }
        return -1;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) != -1;
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            throw new ArrayIndexOutOfBoundsException("size" + size + " index:" + index);
        }
    }

    /**
     * 根据下标找到对应的节点
     * @param index
     * @return
     */
    private Node<E> getNodeByIndex(int index) {
        rangeCheck(index);
        Node<E> node = first;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("size:").append(size).append(" [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                stringBuilder.append(",");
            }
            stringBuilder.append(getNodeByIndex(i).element);
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
